package com.zklh.book.system.repo.impl;

import com.zklh.book.system.model.Book;
import com.zklh.book.system.repo.BookRepo;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 图书持久层实现
 *
 * @author 坐看落花
 * @version 1.0
 * @date 2021/3/20 19:28
 */
public class BookRepoImpl implements BookRepo {

    private Map<String, Book> bookStore = new LinkedHashMap<>();
    {
        bookStore.put("hlm", new Book("hlm", "红楼梦", 10, 10, "enable"));
        bookStore.put("xyj", new Book("xyj", "西游记", 20, 20, "enable"));
        bookStore.put("sgyy", new Book("sgyy", "三国演义", 5, 5, "deleted"));
        bookStore.put("shz", new Book("shz", "水浒传", 8, 8, "enable"));
        bookStore.put("st", new Book("st", "三体", 7, 7, "disable"));
        bookStore.put("java", new Book("java", "java编程思想", 10, 10, "enable"));
        bookStore.put("czxt", new Book("czxt", "操作系统", 10, 10, "enable"));
        bookStore.put("internet", new Book("internet", "计算机网络", 12, 12, "disable"));
        bookStore.put("hz", new Book("hz", "活着", 11, 11, "enable"));
        bookStore.put("wljs", new Book("wljs", "未来简史", 10, 10, "enable"));
    }

    public Book get(String bookCode) {
        return bookStore.get(bookCode);
    }

    public void save(Book book) {
        Book b = bookStore.get(book.getCode());
        if (b == null) {
            bookStore.put(book.getCode(), book);
            return;
        } else {
            bookStore.put(book.getCode(), book);
        }
    }

    @Override
    public List<Book> findAll() {
        return new ArrayList<>(bookStore.values());
    }

    public Book getone(String bookCode){
        return bookStore.get(bookCode);
    }


}
